home *** CD-ROM | disk | FTP | other *** search
/ isnet Internet / Isnet Internet CD.iso / prog / hiz / 09 / 09.exe / adynware.exe / perl / lib / Test / Harness.pm
Encoding:
Perl POD Document  |  1999-12-28  |  10.0 KB  |  372 lines

  1. package Test::Harness;
  2.  
  3. BEGIN {require 5.002;}
  4. use Exporter;
  5. use Benchmark;
  6. use Config;
  7. use FileHandle;
  8. use strict;
  9.  
  10. use vars qw($VERSION $verbose $switches $have_devel_corestack $curtest
  11.         @ISA @EXPORT @EXPORT_OK);
  12. $have_devel_corestack = 0;
  13.  
  14. $VERSION = "1.1502";
  15.  
  16. @ISA=('Exporter');
  17. @EXPORT= qw(&runtests);
  18. @EXPORT_OK= qw($verbose $switches);
  19.  
  20. format STDOUT_TOP =
  21. Failed Test  Status Wstat Total Fail  Failed  List of failed
  22. ------------------------------------------------------------------------------
  23. .
  24.  
  25. format STDOUT =
  26. @<<<<<<<<<<<<<< @>> @>>>> @>>>> @>>> ^##.##%  @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  27. { $curtest->{name},
  28.                 $curtest->{estat},
  29.                     $curtest->{wstat},
  30.                           $curtest->{max},
  31.                                 $curtest->{failed},
  32.                                      $curtest->{percent},
  33.                                               $curtest->{canon}
  34. }
  35. .
  36.  
  37.  
  38. $verbose = 0;
  39. $switches = "-w";
  40.  
  41. sub runtests {
  42.     my(@tests) = @_;
  43.     local($|) = 1;
  44.     my($test,$te,$ok,$next,$max,$pct,$totok,@failed,%failedtests);
  45.     my $totmax = 0;
  46.     my $files = 0;
  47.     my $bad = 0;
  48.     my $good = 0;
  49.     my $total = @tests;
  50.  
  51.     my $old5lib = $ENV{PERL5LIB};
  52.     local($ENV{'PERL5LIB'}) = join($Config{path_sep}, @INC);
  53.  
  54.     if ($^O eq 'VMS') { $switches =~ s/-(\S*[A-Z]\S*)/"-$1"/g }
  55.  
  56.     my $t_start = new Benchmark;
  57.     while ($test = shift(@tests)) {
  58.     $te = $test;
  59.     chop($te);
  60.     if ($^O eq 'VMS') { $te =~ s/^.*\.t\./[.t./; }
  61.     print "$te" . '.' x (20 - length($te));
  62.     my $fh = new FileHandle;
  63.     $fh->open($test) or print "can't open $test. $!\n";
  64.     my $first = <$fh>;
  65.     my $s = $switches;
  66.     $s .= q[ "-T"] if $first =~ /^#!.*\bperl.*-\w*T/;
  67.     $fh->close or print "can't close $test. $!\n";
  68.     my $cmd = "$^X $s $test|";
  69.     $cmd = "MCR $cmd" if $^O eq 'VMS';
  70.     $fh->open($cmd) or print "can't run $test. $!\n";
  71.     $ok = $next = $max = 0;
  72.     @failed = ();
  73.     while (<$fh>) {
  74.         if( $verbose ){
  75.         print $_;
  76.         }
  77.         if (/^1\.\.([0-9]+)/) {
  78.         $max = $1;
  79.         $totmax += $max;
  80.         $files++;
  81.         $next = 1;
  82.         } elsif ($max && /^(not\s+)?ok\b/) {
  83.         my $this = $next;
  84.         if (/^not ok\s*(\d*)/){
  85.             $this = $1 if $1 > 0;
  86.             push @failed, $this;
  87.         } elsif (/^ok\s*(\d*)/) {
  88.             $this = $1 if $1 > 0;
  89.             $ok++;
  90.             $totok++;
  91.         }
  92.         if ($this > $next) {
  93.             push @failed, $next..$this-1;
  94.         } elsif ($this < $next) {
  95.             warn "Confused test output: test $this answered after test ", $next-1, "\n";
  96.             $next = $this;
  97.         }
  98.         $next = $this + 1;
  99.         }
  100.     }
  101.     $fh->close; # must close to reap child resource values
  102.     my $wstatus = $?;
  103.     my $estatus;
  104.     $estatus = ($^O eq 'VMS'
  105.                ? eval 'use vmsish "status"; $estatus = $?'
  106.                : $wstatus >> 8);
  107.     if ($wstatus) {
  108.         my ($failed, $canon, $percent) = ('??', '??');
  109.         print "dubious\n\tTest returned status $estatus (wstat $wstatus)\n";
  110.         print "\t\t(VMS status is $estatus)\n" if $^O eq 'VMS';
  111.         if (corestatus($wstatus)) { # until we have a wait module
  112.         if ($have_devel_corestack) {
  113.             Devel::CoreStack::stack($^X);
  114.         } else {
  115.             print "\ttest program seems to have generated a core\n";
  116.         }
  117.         }
  118.         $bad++;
  119.         if ($max) {
  120.           if ($next == $max + 1 and not @failed) {
  121.         print "\tafter all the subtests completed successfully\n";
  122.         $percent = 0;
  123.         $failed = 0;    # But we do not set $canon!
  124.           } else {
  125.         push @failed, $next..$max;
  126.         $failed = @failed;
  127.         (my $txt, $canon) = canonfailed($max,@failed);
  128.         $percent = 100*(scalar @failed)/$max;
  129.         print "DIED. ",$txt;
  130.           }
  131.         }
  132.         $failedtests{$test} = { canon => $canon,  max => $max || '??',
  133.                     failed => $failed, 
  134.                     name => $test, percent => $percent,
  135.                     estat => $estatus, wstat => $wstatus,
  136.                   };
  137.     } elsif ($ok == $max && $next == $max+1) {
  138.         if ($max) {
  139.         print "ok\n";
  140.         } else {
  141.         print "skipping test on this platform\n";
  142.         }
  143.         $good++;
  144.     } elsif ($max) {
  145.         if ($next <= $max) {
  146.         push @failed, $next..$max;
  147.         }
  148.         if (@failed) {
  149.         my ($txt, $canon) = canonfailed($max,@failed);
  150.         print $txt;
  151.         $failedtests{$test} = { canon => $canon,  max => $max,
  152.                     failed => scalar @failed,
  153.                     name => $test, percent => 100*(scalar @failed)/$max,
  154.                     estat => '', wstat => '',
  155.                       };
  156.         } else {
  157.         print "Don't know which tests failed: got $ok ok, expected $max\n";
  158.         $failedtests{$test} = { canon => '??',  max => $max,
  159.                     failed => '??', 
  160.                     name => $test, percent => undef,
  161.                     estat => '', wstat => '',
  162.                       };
  163.         }
  164.         $bad++;
  165.     } elsif ($next == 0) {
  166.         print "FAILED before any test output arrived\n";
  167.         $bad++;
  168.         $failedtests{$test} = { canon => '??',  max => '??',
  169.                     failed => '??',
  170.                     name => $test, percent => undef,
  171.                     estat => '', wstat => '',
  172.                   };
  173.     }
  174.     }
  175.     my $t_total = timediff(new Benchmark, $t_start);
  176.     
  177.     if ($^O eq 'VMS') {
  178.     if (defined $old5lib) {
  179.         $ENV{PERL5LIB} = $old5lib;
  180.     } else {
  181.         delete $ENV{PERL5LIB};
  182.     }
  183.     }
  184.     if ($bad == 0 && $totmax) {
  185.         print "All tests successful.\n";
  186.     } elsif ($total==0){
  187.     die "FAILED--no tests were run for some reason.\n";
  188.     } elsif ($totmax==0) {
  189.     my $blurb = $total==1 ? "script" : "scripts";
  190.     die "FAILED--$total test $blurb could be run, alas--no output ever seen\n";
  191.     } else {
  192.     $pct = sprintf("%.2f", $good / $total * 100);
  193.     my $subpct = sprintf " %d/%d subtests failed, %.2f%% okay.",
  194.     $totmax - $totok, $totmax, 100*$totok/$totmax;
  195.     my $script;
  196.     for $script (sort keys %failedtests) {
  197.       $curtest = $failedtests{$script};
  198.       write;
  199.     }
  200.     if ($bad) {
  201.         die "Failed $bad/$total test scripts, $pct% okay.$subpct\n";
  202.     }
  203.     }
  204.     printf("Files=%d,  Tests=%d, %s\n", $files, $totmax, timestr($t_total, 'nop'));
  205.  
  206.     return ($bad == 0 && $totmax) ;
  207. }
  208.  
  209. my $tried_devel_corestack;
  210. sub corestatus {
  211.     my($st) = @_;
  212.     my($ret);
  213.  
  214.     eval {require 'wait.ph'};
  215.     if ($@) {
  216.       SWITCH: {
  217.         $ret = ($st & 0200); # Tim says, this is for 90%
  218.     }
  219.     } else {
  220.     $ret = WCOREDUMP($st);
  221.     }
  222.  
  223.     eval { require Devel::CoreStack; $have_devel_corestack++ } 
  224.       unless $tried_devel_corestack++;
  225.  
  226.     $ret;
  227. }
  228.  
  229. sub canonfailed ($@) {
  230.     my($max,@failed) = @_;
  231.     my %seen;
  232.     @failed = sort {$a <=> $b} grep !$seen{$_}++, @failed;
  233.     my $failed = @failed;
  234.     my @result = ();
  235.     my @canon = ();
  236.     my $min;
  237.     my $last = $min = shift @failed;
  238.     my $canon;
  239.     if (@failed) {
  240.     for (@failed, $failed[-1]) { # don't forget the last one
  241.         if ($_ > $last+1 || $_ == $last) {
  242.         if ($min == $last) {
  243.             push @canon, $last;
  244.         } else {
  245.             push @canon, "$min-$last";
  246.         }
  247.         $min = $_;
  248.         }
  249.         $last = $_;
  250.     }
  251.     local $" = ", ";
  252.     push @result, "FAILED tests @canon\n";
  253.     $canon = "@canon";
  254.     } else {
  255.     push @result, "FAILED test $last\n";
  256.     $canon = $last;
  257.     }
  258.  
  259.     push @result, "\tFailed $failed/$max tests, ";
  260.     push @result, sprintf("%.2f",100*(1-$failed/$max)), "% okay\n";
  261.     my $txt = join "", @result;
  262.     ($txt, $canon);
  263. }
  264.  
  265. 1;
  266. __END__
  267.  
  268. =head1 NAME
  269.  
  270. Test::Harness - run perl standard test scripts with statistics
  271.  
  272. =head1 SYNOPSIS
  273.  
  274. use Test::Harness;
  275.  
  276. runtests(@tests);
  277.  
  278. =head1 DESCRIPTION
  279.  
  280. Perl test scripts print to standard output C<"ok N"> for each single
  281. test, where C<N> is an increasing sequence of integers. The first line
  282. output by a standard test script is C<"1..M"> with C<M> being the
  283. number of tests that should be run within the test
  284. script. Test::Harness::runtests(@tests) runs all the testscripts
  285. named as arguments and checks standard output for the expected
  286. C<"ok N"> strings.
  287.  
  288. After all tests have been performed, runtests() prints some
  289. performance statistics that are computed by the Benchmark module.
  290.  
  291. =head2 The test script output
  292.  
  293. Any output from the testscript to standard error is ignored and
  294. bypassed, thus will be seen by the user. Lines written to standard
  295. output containing C</^(not\s+)?ok\b/> are interpreted as feedback for
  296. runtests().  All other lines are discarded.
  297.  
  298. It is tolerated if the test numbers after C<ok> are omitted. In this
  299. case Test::Harness maintains temporarily its own counter until the
  300. script supplies test numbers again. So the following test script
  301.  
  302.     print <<END;
  303.     1..6
  304.     not ok
  305.     ok
  306.     not ok
  307.     ok
  308.     ok
  309.     END
  310.  
  311. will generate 
  312.  
  313.     FAILED tests 1, 3, 6
  314.     Failed 3/6 tests, 50.00% okay
  315.  
  316. The global variable $Test::Harness::verbose is exportable and can be
  317. used to let runtests() display the standard output of the script
  318. without altering the behavior otherwise.
  319.  
  320. =head1 EXPORT
  321.  
  322. C<&runtests> is exported by Test::Harness per default.
  323.  
  324. =head1 DIAGNOSTICS
  325.  
  326. =over 4
  327.  
  328. =item C<All tests successful.\nFiles=%d,  Tests=%d, %s>
  329.  
  330. If all tests are successful some statistics about the performance are
  331. printed.
  332.  
  333. =item C<FAILED tests %s\n\tFailed %d/%d tests, %.2f%% okay.>
  334.  
  335. For any single script that has failing subtests statistics like the
  336. above are printed.
  337.  
  338. =item C<Test returned status %d (wstat %d)>
  339.  
  340. Scripts that return a non-zero exit status, both C<$? E<gt>E<gt> 8> and C<$?> are
  341. printed in a message similar to the above.
  342.  
  343. =item C<Failed 1 test, %.2f%% okay. %s>
  344.  
  345. =item C<Failed %d/%d tests, %.2f%% okay. %s>
  346.  
  347. If not all tests were successful, the script dies with one of the
  348. above messages.
  349.  
  350. =back
  351.  
  352. =head1 SEE ALSO
  353.  
  354. See L<Benchmark> for the underlying timing routines.
  355.  
  356. =head1 AUTHORS
  357.  
  358. Either Tim Bunce or Andreas Koenig, we don't know. What we know for
  359. sure is, that it was inspired by Larry Wall's TEST script that came
  360. with perl distributions for ages. Numerous anonymous contributors
  361. exist. Current maintainer is Andreas Koenig.
  362.  
  363. =head1 BUGS
  364.  
  365. Test::Harness uses $^X to determine the perl binary to run the tests
  366. with. Test scripts running via the shebang (C<#!>) line may not be
  367. portable because $^X is not consistent for shebang scripts across
  368. platforms. This is no problem when Test::Harness is run with an
  369. absolute path to the perl binary or when $^X can be found in the path.
  370.  
  371. =cut
  372.